<template>
  <div style="width: 100%;height: 100vh; background-color: blueviolet; overflow: hidden">
    <div style="width: 300px;margin: 100px auto">
      <div style="color: #cccccc;text-align: center;font-size: 30px;padding: 30px 0" > 欢迎登陆 </div>
      <el-form ref="form" :model="form" size="large" :rules="rules">
        <el-form-item prop="name">
          <el-input style="font-size: 20px" prefix-icon="el-icon-user-solid" v-model="form.name" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input style="font-size: 20px" prefix-icon="el-icon-lock-solid" show-password v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="login">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import {ElMessage} from "element-plus";

export default {
  name: "Login",
  data() {
    return {
      form:{},
      rules: {
        name: [
          {required: true, message: '用户名不能为空', trigger: 'blur',},
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur',},
        ]
      },

    }
  },
  methods:{
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/login",this.form).then(res => {
            if (res.code == 200) {
              ElMessage({
                message: '登录成功.',
                type: 'success',
              })
              console.log(res)
              sessionStorage.setItem("user",JSON.stringify(res.data))  // 缓存用户信息
              this.$router.push("/")  // 登录跳转
            } else {
              ElMessage.error('登录失败：'+res.msg)
            }
          })
        }
      })
    },

  }
}
</script>

<style scoped>

</style>